package com.jwthhealth_pub.old.report.view.adapter;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.report.model.resbody.HraDataRes;
import com.jwthhealth_pub.old.report.view.IndicalPhysicalActivity;
import com.jwthhealth_pub.old.report.view.ReportPhysicalActivity;

import java.util.List;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

public class ReportPhisicalAdapter extends RecyclerView.Adapter {

    public static String TAG = makeLogTag(ReportPhisicalAdapter.class);
    private final String id;

    private ReportPhysicalActivity mActivity;
    private List<HraDataRes.DataBean.ZjBean.SonBeanX.SonBean> mData;

    public ReportPhisicalAdapter(ReportPhysicalActivity mActivity, List<HraDataRes.DataBean.ZjBean.SonBeanX.SonBean> son, String id) {
        this.mActivity = mActivity;
        this.mData = son;
        this.id = id;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == type.foot.ordinal()) {
            view = View.inflate(mActivity, R.layout.item_report_detial_value_top, null);
            return new FootViewHolder(view);
        } else {
            view = View.inflate(mActivity, R.layout.item_report_detial_value_top, null);
            return new HeadViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            //head
            HeadViewHolder vh = (HeadViewHolder) holder;
            Resources resources = mActivity.getResources();
            vh.name.setText(resources.getString(R.string.reprot_physical_describe_start));
            vh.commonValue.setText(resources.getString(R.string.reprot_physical_describe_middle));
            vh.testValue.setText(resources.getString(R.string.reprot_physical_describe_end));
            int color = ContextCompat.getColor(mActivity, R.color.primarycolor);
            vh.name.setTextColor(color);
            vh.commonValue.setTextColor(color);
            vh.testValue.setTextColor(color);
//        } else if (position == getItemCount() - 1 && getItemCount() >= 7) {//不必再判断数据是否大于5，每一个条目都有footview
        } else if (position == getItemCount() - 1) {
            //foot
            final FootViewHolder vh = (FootViewHolder) holder;

            if (getItemCount() < 7){
                //如果数据小于五条，则隐藏footlayout
                vh.headLayout.setVisibility(View.VISIBLE);
                vh.iconImg.setVisibility(View.GONE);
            }
            if (getItemCount() == 7) {
                vh.headLayout.setVisibility(View.VISIBLE);
                vh.iconImg.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.show_more_arrow));
            } else {
                vh.headLayout.setVisibility(View.VISIBLE);
                vh.iconImg.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.show_preview_arrow));
            }

            vh.iconIndex.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getItemCount() == 7) {
                        mActivity.showAllData();
                    } else {
                        mActivity.showPreviewData();
                    }
                }
            });

            vh.indivator.setVisibility(View.VISIBLE);
            vh.indivator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mActivity, IndicalPhysicalActivity.class);
                    intent.putExtra("id", id);
                    mActivity.startActivity(intent);
                }
            });
        } else {
            //body
            HeadViewHolder vh = (HeadViewHolder) holder;

            int _pos = --position;
            if (_pos > mData.size() || _pos == -1) return;

            HraDataRes.DataBean.ZjBean.SonBeanX.SonBean sonBean = mData.get(_pos);
            vh.name.setText(sonBean.getC_name());
            int color = ContextCompat.getColor(mActivity, R.color.textColorBlack);
            vh.name.setTextColor(color);
            vh.commonValue.setTextColor(color);
            vh.testValue.setTextColor(color);
            vh.commonValue.setText(sonBean.getLook_value());
            vh.testValue.setText(sonBean.getValue());
        }

    }

    @Override
    public int getItemCount() {
        //不必再判断数据是否大于5，每一个条目都有footview
//        if (mData.size() < 5) {
//            return mData.size() + 2;
//        } else
//            return mData.size() + 2;
        return mData.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return type.head.ordinal();
//        } else if (position == getItemCount() - 1 && getItemCount() >= 7) {
        } else if (position == getItemCount() - 1) {
            return type.foot.ordinal();
        } else {
            return type.body.ordinal();
        }
    }


    private class HeadViewHolder extends RecyclerView.ViewHolder {
        TextView name, commonValue, testValue;

        HeadViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            commonValue = itemView.findViewById(R.id.commonvalue);
            testValue = itemView.findViewById(R.id.test_vlaue);
        }
    }

    private class FootViewHolder extends RecyclerView.ViewHolder {

        //        LinearLayout clickLayout;
        TextView clickLayout;
        RelativeLayout headLayout;
        RelativeLayout iconIndex;
        ImageView iconImg;
        // TODO: 2017/10/23 指标说明
        TextView indivator;

        FootViewHolder(View itemView) {
            super(itemView);
            clickLayout = itemView.findViewById(R.id.test_vlaue);
            headLayout = itemView.findViewById(R.id.head_layout);
            iconIndex = itemView.findViewById(R.id.arrow_icon_layout);
            iconImg = itemView.findViewById(R.id.arrow_icon);
            indivator = itemView.findViewById(R.id.indicator);
        }
    }

    enum type {
        head, body, foot
    }
}
